package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AdressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class AdressBookServiceImpl implements AdressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void save(AddressBook addressBook) {
        addressBook.setIsDefault(0);
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);
        addressBookMapper.save(addressBook);

    }

    @Override
    public List<AddressBook> list(Long currentId) {
        List<AddressBook> list = addressBookMapper.list(currentId);
        return  list;
    }

    @Override
    public void update(AddressBook addressBook) {

        addressBookMapper.update(addressBook);
    }

    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = addressBookMapper.selectById(id);
        return addressBook;
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        Long currentId = BaseContext.getCurrentId();
        addressBookMapper.updateIsDefaultByUserId(currentId);
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }

    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }

    @Override
    public AddressBook getDefault() {
        Long currentId = BaseContext.getCurrentId();
        AddressBook addressBook = addressBookMapper.getDefault(currentId);
        return addressBook;
    }
}
